import { getSessionItem, ishasAgentId, setSessionItem, removeSessionItem } from "@/basa/utils";
import {
  orderList,getCurrencyTypeDict
} from "../api/template-request";
// import {
//   listPageOrder,
//   removeOrder,
//   enteringRevokeApproval
// } from "../api/orderEntry";
// import { formatNumber } from "@/basa/utils";

export default {
  data() {
    return {
      approveStatusList:[
        {
          dicKey:'0',
          dicValue:'待审批'
        },
        {
          dicKey:'1',
          dicValue:'已审批'
        }
      ],
      approveTypeList:[
        {
          dicKey:'S0601',
          dicValue:'付款申请'
        },
        {
          dicKey:'S0603',
          dicValue:'付款登记'
        }
      ],
      paymentModeList:[
        {
          dicKey:1,
          dicValue:'银行承兑'
        },
        {
          dicKey:2,
          dicValue:'商业承兑'
        },
        {
          dicKey:3,
          dicValue:'供应链金融'
        },
        {
          dicKey:4,
          dicValue:'现金'
        },
      ],
      approveStatusBillList:[
        // {
        //   dicKey:'1',
        //   dicValue:'草稿'
        // },
        // {
        //   dicKey:'2',
        //   dicValue:'待提交'
        // },
        // {
        //   dicKey:'3',
        //   dicValue:'审批中'
        // },
        {
          dicKey:2,
          dicValue:'审批通过'
        },
        {
          dicKey:3,
          dicValue:'审批退回'
        },
        {
          dicKey:4,
          dicValue:'审批不通过'
        },
        {
          dicKey:5,
          dicValue:'协办'
        },
        {
          dicKey:6,
          dicValue:'转审'
        },
        // {
        //   dicKey:'7',
        //   dicValue:'变更草稿'
        // },
        // {
        //   dicKey:'8',
        //   dicValue:'变更待提交'
        // },
        // {
        //   dicKey:'9',
        //   dicValue:'变更审批中'
        // },
        // {
        //   dicKey:'10',
        //   dicValue:'变更审批通过'
        // },
        // {
        //   dicKey:'11',
        //   dicValue:'变更审批不通过'
        // },
        // {
        //   dicKey:'12',
        //   dicValue:'变更审批退回'
        // },
      ],
      // 高级搜索相关
      isShowSearch: true,
      formData: {
        bizStatus: "",
        pageNo: 1,
        pageSize: 10,
        paymentCode:"",
        paymentName:"",
        contractCode:"",
        formalContractCode:"",
        contractName:"",
        reportInOrgName:"",
        payee:"",
        paymentMode:"",
        currencyCode:"",
        status:"",
        approveStatus:""
        // businessBillTypes:""
      },
      // Tab切换
      isActive: 0,
      focuses: [
        { focus: "approvalall" },
        { focus: "pendingapproval" },
        { focus: "approved" }
      ],
      tableData: [
        // {
        //   orderNo: "DD202105111002",
        //   approvalType: "订单录入",
        //   orderName: "项目订单",
        //   thirdCategoriesOfName: "热带钢筋",
        //   partyBName: "福建建筑",
        //   writename: "中交集团",
        //   orderDeliveryTime: "2020-02-02",
        //   bizStatus: "已审批"
        // }
      ],
      tableColumnData: [
        {
          label: "序号",
          type: "index",
          fixed: true
        },
        {
          label: "付款申请单编号",
          prop: "businessData.paymentCode",
          width: "150",
          fixed: true
        },
        {
          label: "付款登记单编号",
          prop: "businessData.registerCode",
          width: "150",
        },
        {
          label: "审批类型",
          prop: "todoData.businessBillTypeName",
          width: "150",
        },
        {
          label: "付款单名称",
          prop: "businessData.paymentName",
          width: "300"
        },
        {
          label: "合同编号",
          prop: "businessData.contractCode",
          width: "100"
        },
        {
          label: "合同正式编号",
          prop: "businessData.formalContractCode",
          width: "100"
        },
        {
          label: "合同名称",
          prop: "businessData.contractName",
          width: "200"
        },
        {
          label: "填报单位",
          prop: "businessData.reportInOrgName",
          width: "200"
        },
        {
          label: "收款人",
          prop: "businessData.payee",
          width: "200"
        },
        {
          label: "付款方式",
          prop: "businessData.paymentMode",
          width: 100
        },
        {
          label: "申请付款金额",
          prop: "businessData.paymentMoneyString",
          width: 100
        },
        {
          label: "登记付款金额",
          prop: "businessData.registerPriceString",
          width: 120
        },
        {
          label: "币种",
          prop: "businessData.currencyName",
          width: 70
        },
        {
          label: "编制人",
          prop: "businessData.reportUsername",
          width: 70
        },
        {
          label: "编制日期",
          prop: "businessData.reportTime",
          width: 100
        },
        {
          label: "审批状态",
          prop: "todoData.workItemStatusString",
          width: 70
        },
        // {
        //   label: "审批结果",
        //   prop: "businessData.approveStatus",
        //   width: 70,
        //   formatterName:"approveStatus"
        // },
      ],
      // 表格数据处理
      formatterList: {
        approveStatus(row){
          console.log('row>>>>',row)
          switch(row.businessData.approveStatus){
            // case "1":
            //   return '草稿';
            // case "2":
            //   return '待提交';
            // case "3":
            //   return '审批中';
            case "4":
              return '审批通过';
            // case "5":
            //   return '审批不通过';
            case "6":
              return '审批退回';
            // case "7":
            //   return '变更草稿';
            // case "8":
            //   return '变更待提交';
            // case "9":
            //   return '变更审批中';
            // case "10":
            //   return '变更审批通过';
            // case "11":
            //   return '变更审批不通过';
            // case "12":
            //   return '变更审批退回';
            
          }
        },
      },
      // 分页相关
      isShowPagination: false,
      totalNum: 0,
      pageNoCode: this.getLocalPageNo(),
      pageSizeCode: this.getLocalPageSize(),
      tableName: this.tableList.applicationApprovalList,
      bizStatusType: {
        1: "银行承兑",
        2: "商业承兑",
        3: "供应链金融",
        4: "现金"
      },
      bizStatusSearch: [
        {
          label: "待提交",
          value: "2"
        },
        {
          label: "审批中",
          value: "3"
        },
        {
          label: "已撤回",
          value: "4"
        },
        {
          label: "审批退回",
          value: "6"
        }
      ],
      buttonBool: false,
      formDataSearch: "",
      localSearchData: {},
      currencyNameList:[]
    };
  },
  created() {
    getCurrencyTypeDict({}).then(res =>{
      console.log('币种',res)
      this.currencyNameList = res.data.data
      this.currencyNameList.forEach(v =>{
        v.dicKey = Number(v.dicKey)
      })
    })
  },
  methods: {
    searchSelect(){},
    searchSelect1(){},
    search(searchData) {
      let type = ''
      switch(this.isActive){
        case 0:
          type = 'all';
          break;
        case 1:
          type = 'pending';
          break;
        case 2:
          type = 'approved';
          break;

      }
      // return type;
     
      this.pageNoCode = 1;
      this.formDataSearch = searchData;
      this.localSearchData = JSON.parse(JSON.stringify(searchData));
      setSessionItem("pageNo", this.tableName, {
        pageNo: this.pageNoCode,
        searchForm: this.localSearchData
      });
      this.getList(this.formDataSearch,type);
    },
    // 列表数据获取
    getList(reqObj,type) {
      console.log('reqObj',reqObj)
      // if(type == 'approved'){
      //   this.tableColumnData.push(
      //     {
      //       label: "审批结果",
      //       prop: "businessData.approveStatus",
      //       width: 70,
      //       formatterName:"approveStatus"
      //     },
      //   )
      // }
      let obj = {};
      obj = typeof reqObj == "object" ? reqObj : obj;
      obj.pageNo = this.pageNoCode;
      obj.pageSize = this.pageSizeCode;
      obj.isShowPageLoading = true;
      // this.formData.pageSize = this.pageSizeCode;
      // this.formData.pageNo = this.pageNoCode;
      obj.businessBillTypes = this.formData.businessBillTypes?[this.formData.businessBillTypes]:[]
      obj.operResultTypes = this.formData.approveStatus == 3?[3,9]:!this.formData.approveStatus?[]:[this.formData.approveStatus]
        orderList(obj,type).then(res => {
          this.isShowPagination = res.data.data.recordsTotal > 0;
          // var arr = []
          // for (let i in res.data.data.rows) {
          //   let b  = {...res.data.data.rows[i].businessData,...res.data.data.rows[i].todoData}
          //   arr.push(b); //属性
          //   //arr.push(obj[i]); //值
          // }
          // this.tableData = arr;
          this.tableData = res.data.data.rows
          console.log("数量保留三位", this.tableData);
          this.tableData.map(e => {
            e.businessData.reportTime = e.businessData.reportTime
              ? e.businessData.reportTime.substr(0, 10)
              : "";
          });
          this.tableData.map(e => {
            e.businessData.paymentMode = this.bizStatusType[e.businessData.paymentMode];
            // e.orderQuantity = formatNumber(e.orderQuantity, 3);
          });
          this.totalNum = res.data.data.recordsTotal;
          this.pageNoCode = res.data.data.pageNo;
        });
    },
    // 提示弹窗
    deleteMessageBox(obj, params, status, callBack) {
      this.$confirm(obj.content, obj.title, {
        distinguishCancelAndClose: true,
        confirmButtonText: obj.confirmButtonText,
        cancelButtonText: "取消",
        customClass: "ccui-message-box-custom"
      })
        .then(() => {
          callBack(params, status);
        })
        .catch(e => {
          console.log(e);
        });
    },
    // 删除
    // removeOrder(params, status) {
    //   removeOrder(params).then(res => {
    //     this.callBackThenMessage(res, status);
    //   });
    // },
    // 撤回
    // enteringRevokeApproval(params, status) {
    //   enteringRevokeApproval(params).then(res => {
    //     this.callBackThenMessage(res, status);
    //   });
    // },
    // 删除 / 撤回成功提示message
    callBackThenMessage(res, status) {
      let { code, message } = res.data;
      this.$message({
        message: message,
        type: code === "0" ? "success" : "error",
        showClose: true,
        ccuiCustom: true
      });
      this.getList(status);
    },
    // 分页相关功能
    // 显示条数更改
    handleSizeChange(val, id) {
      this.pageNoCode = 1;
      if (val !== "none") {
        this.pageSizeCode = val;
        setSessionItem("pageSize", this.tableName, val);
      }
      this.getList(this.formDataSearch,id);
    },
    // 当前页码更改
    handleCurrentChange(val, id) {
      // debugger
      let searchFormData = getSessionItem("pageNo", this.tableName) || {};
      setSessionItem("pageNo", this.tableName, {
        pageNo: val,
        searchForm: searchFormData.searchForm || {}
      });
      this.formDataSearch = {
        ...this.formDataSearch,
        ...searchFormData.searchForm
      };
      // 跳转翻页都会调这个方法
      this.pageNoCode = val;
      console.log('this.pageNoCode',this.pageNoCode)
      this.getList(this.formDataSearch,id);
    },
    // 跳转详情
    handleGoDetail(r) {
      this.$router.push("/orderManagement/entry/detail/" + r.id);
    },
    // 跳转编辑
    handleGoEdit(r) {
      // this.$router.push("/orderManagement/approval/approval-operation" + r.id);
      const {todoData} = r
      this.$store.dispatch("setBusinessList", todoData);
      const businessList = JSON.stringify(todoData);
      sessionStorage.setItem("businessList", businessList)
      console.log(r)
      const params = {
        id:todoData.businessBillId,
        status: "look"
      };
      this.$router.push({
          path:`/paymentManagement/paymentapproval/examineandapprove/${JSON.stringify(params)}`,
          query:{
            businessBillTypeCode:todoData.businessBillTypeCode
          }
        }
      );
    },
    // 跳转新增
    handleGoNew() {
      this.$router.push("/orderManagement/entry/add");
    },
    // Tab切换
    tabChange(data) {
      console.log(data, "Tab切换");
      this.$router.push(`/paymentManagement/paymentapproval/${data}`);
    },
    // 读取当前页缓存
    getLocalPageNo() {
      let localData = getSessionItem("pageNo", this.tableList.applicationApprovalList);
      return (localData && localData.pageNo) || 1;
    },
    // 获取默认分页
    getLocalPageSize() {
      return getSessionItem("pageSize", this.tableList.applicationApprovalList) || 10;
    },
    // 单元格点击跳转详情
    cellClick(r, column) {
      const params = {
        id: r.businessData.paymentId,//付款申请
        status: "look"
      };
      const params1 = {
        id: r.businessData.paymentRegisterId,//付款登记
        status: "look"
      };
      // if (column.label === "付款申请单编号" || column.label === "付款登记单编号") {
        // if(r.todoData.businessBillTypeCode == 'S0601'){
        //   this.$router.push({
        //     path:`/paymentManagement/application/detail/${JSON.stringify(params)}`,
        //     query:{
        //       type:'sunyao'
        //     }
        //   });
        // }else{
        //   this.$router.push({
        //     path:`/paymentManagement/applicationChange/detail/${JSON.stringify(params)}`,
        //     query:{
        //       type:'sunyao'
        //     }
        //   });
        // }


        if(column.label === "付款申请单编号" && r.businessData.paymentId){
          this.$router.push({
            path:`/paymentManagement/application/detail/${JSON.stringify(params)}`,
            query:{
              type:'sunyao'
            }
          });
        }else if(column.label === "付款登记单编号" && r.businessData.paymentRegisterId){
          this.$router.push({
            path:`/paymentManagement/applicationChange/detail/${JSON.stringify(params1)}`,
            query:{
              type:'sunyao'
            }
          });
        }
        
      // }
    },
    cellStyle({ row, column, rowIndex, columnIndex }) {
      if (column.label == "付款申请单编号" || column.label == "付款登记单编号") {
        return { color: "#0089EF", cursor: "pointer" };
      }
    }
  },
  beforeRouteLeave(to, from, next) {
    removeSessionItem("pageNo");
    for (let key in this.$vnode.parent.parent.componentInstance.cache) {
      delete this.$vnode.parent.parent.componentInstance.cache[
        ishasAgentId(to.path, key)
      ];
    }
    next();
  },
  // activated() {
  //   if (Object.keys(this.localSearchData).length > 0) {
  //     setSessionItem("pageNo", this.tableName, {
  //       pageNo: this.pageNoCode,
  //       searchForm: this.localSearchData
  //     });
  //   }
  //   let localData = getSessionItem("pageNo", this.tableList.listqueryList);
  //   let data = localData && localData.searchForm;
  //   this.getList({ ...this.formDataSearch, ...data });
  // }
};
